package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmplyeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.DepartmentService;
import cn.wolfcode.service.EmployeeService;
import cn.wolfcode.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmplyeeQueryObject qo) {

        PageInfo<Employee> pageResult = employeeService.query(qo);
        model.addAttribute("pageResult", pageResult);

        List<Department> depts = departmentService.selectAll();
        model.addAttribute("depts", depts);
        return "employee/list";
    }


    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null){
            employeeService.delete(id);
        }
        return "redirect:/employee/list.do";
    }

    @RequestMapping("/input")
    public String input(Long id,Model model) {
        List<Department> depts = departmentService.selectAll();
        model.addAttribute("depts", depts);

        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles",roles);

        if (id != null){
            Employee employee = employeeService.selectById(id);
            model.addAttribute("employee",employee);
        }

        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
    try{
        if (employee.getId() != null){
            employeeService.update(employee,ids);
        }else {
            employeeService.save(employee,ids);
        }
        return new JsonResult();
    }catch(Exception e){
        e.printStackTrace();
        return new JsonResult(false,"操作失败");
    }
    }



    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids){
        try{
            employeeService.bathcDelete(ids);
            return new JsonResult();
        }catch(Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Boolean> checkName(Long id ,String name){  // valid:true
        Employee empl = employeeService.selectById(id);
        HashMap<String, Boolean> map = new HashMap<>();
        if (id == null){
            Employee employee = employeeService.selectByName(name);
            map.put("valid",employee==null);
            return map;
        }else{
            String A_name = empl.getName();
            if(name.equals(A_name)){
                map.put("valid",true);
                return map;
            }
        }
        Employee employee = employeeService.selectByName(name);
        map.put("valid",employee==null);
        return map;
    }



    //员工数据导出操作
    @RequestMapping("/exporXls")
    public void exporXls(HttpServletResponse response) throws IOException {
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        Workbook workbook = employeeService.exproXls();
        //输出文件
        workbook.write(response.getOutputStream());
    }

    //员工导入
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        employeeService.imporXls(file);
        return new JsonResult();
    }











    //跳转到修改密码的页面
    @RequestMapping("/setPassword")
    public String forword(){
        return "employee/updatePwd";
    }

    //修改密码操作
    @RequestMapping("/updatePassword")
    @ResponseBody
    public JsonResult updatePw(String oldPassword, String newPassword, Long id, HttpSession session){
        Employee employee = employeeService.selectByPassword(oldPassword,id);
        if (employee == null){
            return new JsonResult(false,"原密码不正确");
        }else{
            employeeService.setPassword(id,newPassword);
            session.invalidate();
            return new JsonResult();
        }
    }


    @RequestMapping("/overlordPassword")
    public String savePassword(Long id, HttpSession session){
        Employee employee = employeeService.selectById(id);
        session.setAttribute("employee",employee);
        return "/employee/resetPwd";
    }

    @RequestMapping("/resetPassword")
    @ResponseBody
    public JsonResult resetPassword(String newPassword,Long id){
        employeeService.setPassword(id,newPassword);
        return new JsonResult();
    }

}
